<?php
/**
 * Created by PhpStorm.
 * User: shkj
 * Date: 2020/4/18
 * Time: 22:02
 */

namespace app\api\model;


use think\Model;
use think\Db;
use app\api\service\Wechat;
class User extends Model
{

    protected $autoWriteTimestamp = true;

    protected $createTime='create_time';
    protected $dateFormat='Y年m月d日';
    //开启自动设置
    protected $auto = [];
    protected $insert = ['create_tiame'];


    public static function getByOpenID($openid){
        $user = self::where('openid','=',$openid)
            ->find();
        return $user;
    }

    public  function vali($data){
    
      $status = self::where('id',$data['uid'])->where('pay_pass',md5($data['val']))->find();
        if($status){
            $price = db('order')->field('order_total_price')->where('id',$data['order_id'])->find();
            // dump($status);
            $money = $status['money'] - $price['order_total_price'];
              $integral =  1;
              //收货的时候就有积分加
            // $integral =  $status['integral'] + round($price['order_total_price'] * 0.15);
     
            // die;
            if($money>0 || $money==0){
                $moneys = self::where('id',$data['uid'])->update(['money'=>$money]);
                $integral = self::where('id',$data['uid'])->update(['integral'=>$integral]);
               $res = $this->pay($data);
            }else{
              return_json(200,'余额不足','余额不足');
            }
        }else{
            return_json(400,'密码错误','密码错误');
        }
    }

    private function pay($data){

        $order = db('order')->where('user_id',$data['uid'])->where('id',$data['order_id'])->update(['status'=>1]);
         if($order){
            $wechat = new Wechat();
            $wechat->sends($data);
        }else{
            return_json(400,'失败',false);
        }
    }
    


}